package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.RxHelper;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.login.activity.InputAuthCodeActivity;
import com.umeng.socialize.UMShareAPI;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;

public class ModifyPasswordActivity extends BaseActivity {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.txt_phone)
    TextView txt_phone;

    //intent
    private Intent intent = null;
    //手机号
    private String phone;
    //type
    private int type = 0;
    //times
    private int currentTimes = 60;
    //request
    private final int REQUEST_CODE = 0x01;
    //code
    private String code = null;

    @Override
    protected int initLayout() {
        return R.layout.activity_modify_password_view;
    }

    @Override
    protected void initView() {
        intent = getIntent();
        if (intent != null) {
            type = intent.getIntExtra(Constant.INTENT_TYPE, Constant.CHANGE_PWD);
            phone = intent.getStringExtra(Constant.INTENT_DATA);
            txt_top_title.setText(type == Constant.CHANGE_PWD ?
                    "修改密码" : "更换手机号码");
            txt_phone.setText(phone);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.layout_back, R.id.txtNextStep})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.txtNextStep:
                intent = new Intent(this, InputAuthCodeActivity.class);
                intent.putExtra(Constant.INTENT_DATA, phone);
                intent.putExtra(Constant.INTENT_TYPE, type);
                intent.putExtra(Constant.INTENT_MESSAGE, currentTimes);
                intent.putExtra(Constant.INTENT_CODE, code);
                startActivityForResult(intent, REQUEST_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE) {
                if (data != null && data.hasExtra(Constant.INTENT_MESSAGE)) {
                    currentTimes = data.getIntExtra(Constant.INTENT_MESSAGE, 0);
                    code = data.getStringExtra(Constant.INTENT_CODE);
                    cutDownTime();
                } else {
                    if (data != null && data.hasExtra(Constant.INTENT_DATA)) {
                        Intent intent = new Intent();
                        intent.putExtra(Constant.INTENT_DATA, data.getStringExtra(Constant.INTENT_DATA));
                        setResult(RESULT_OK, intent);
                    }else {
                        setResult(RESULT_OK);
                    }
                    finish();
                }
            }
        }
    }

    //倒计时操作
    private void cutDownTime() {
        RxHelper.countdown(currentTimes)
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        currentTimes = integer;
                    }
                });
    }
}
